package com.hongyuan.coach.myview.table_view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.GridLayout;
import android.widget.RelativeLayout;

import androidx.core.view.ViewCompat;
import androidx.core.widget.NestedScrollView;

import com.hongyuan.coach.R;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;

public class TableView extends RelativeLayout {

    private static final int DEFAULT_HEADER_COLOR = 0xFF999999;

    private TableHeaderView tableHeaderView;
    private GridLayout tableDataView;

    private TableHeaderAdapter tableHeaderAdapter;
    private TableDataAdapter tableDataAdapter;

    private int headerElevation = 20;
    private int headerColor = DEFAULT_HEADER_COLOR;

    //-------------------------------------------------------------------------------------------
    private SmartRefreshLayout refreshLayout;
    private NestedScrollView scrollView;

    public TableView(Context context) {
        this(context,null);

        initLayoutView();
    }

    public TableView(Context context, AttributeSet attributes) {
        super(context, attributes);

        //setOrientation(LinearLayout.VERTICAL);
        tableHeaderView = new TableHeaderView(getContext());
        tableDataView = new GridLayout(getContext());

        initLayoutView();
    }

    /*
    * 修改为xml布局
    * */
    public void initLayoutView(){
        //-------------------------以下改动--------------------------------------------------------
        View view = View.inflate(getContext(), R.layout.view_table_content, this);
        refreshLayout = view.findViewById(R.id.refresh);
        scrollView = view.findViewById(R.id.scroll);
        setOnRefresh(refreshLayout);
    }

    public void setOnRefresh(SmartRefreshLayout refresh){

        //关闭滚动到底部自动加载
        refresh.setEnableAutoLoadMore(false);
        //初始刷新动画
        refresh.setRefreshHeader(new MaterialHeader(getContext()).setShowBezierWave(true));
        //初始化加载动画
        refresh.setRefreshFooter(new BallPulseFooter(getContext()).setSpinnerStyle(SpinnerStyle.Scale));
        //设置主题颜色
        refresh.setPrimaryColors(getResources().getColor(R.color.transparent));
        refresh.setEnableLoadMore(false);
        refresh.setEnableRefresh(false);
        //默认启用仿苹果越界拖动效果
        refresh.setEnableOverScrollDrag(true);
    }

    public void setTableAdapter(final TableHeaderAdapter tableHeaderAdapter, final TableDataAdapter tableDataAdapter){
        this.tableHeaderAdapter = tableHeaderAdapter;
        this.tableDataAdapter = tableDataAdapter;

        if(isInEditMode()){
            return;
        }

        setHeaderView();

        ViewTreeObserver viewTreeObserver = tableHeaderView.getViewTreeObserver();
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                tableDataAdapter.setTableDataViewWidth(tableHeaderView.getWidth());
                tableHeaderView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                setupTableDataView();
                forceRefresh();
            }
        });
    }

    protected void setHeaderView() {

        tableHeaderView.setAdapter(tableHeaderAdapter);
        //有阴影的背景线
        //tableHeaderView.setBackgroundColor(headerColor);
        tableHeaderView.setId(R.id.table_header_view);

        if (getChildCount() == 2) {
            removeViewAt(0);
        }

        addView(tableHeaderView, 0);
        setHeaderElevation(headerElevation);

        forceRefresh();
    }

    /**
     * Sets the elevation level of the header view. If you are not able to see the elevation shadow
     * you should set a background(-color) to the header.
     *
     * @param elevation
     *         The elevation that shall be set to the table header.
     */
    public void setHeaderElevation(final int elevation) {
        ViewCompat.setElevation(tableHeaderView, elevation);
    }

    private void setupTableDataView() {

        /*final LayoutParams scrollViewLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        NestedScrollView scrollView = new NestedScrollView(getContext());
        scrollView.setLayoutParams(scrollViewLayoutParams);
        addView(scrollView);*/

        final LayoutParams dataViewLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

        tableDataView.setLayoutParams(dataViewLayoutParams);
        tableDataView.setId(R.id.table_data_view);
        tableDataAdapter.setTableDataView(tableDataView);
        scrollView.addView(tableDataView);
    }

    private void forceRefresh() {
        if (tableHeaderView != null) {
            tableHeaderView.invalidate();

        }
        if (tableDataView != null) {
            tableDataView.invalidate();
        }
    }
}
